﻿Imports DevExpress.XtraGrid.Views.Grid
Public Class frmSection
    Dim _mysql As New ClsSQLhelper
    Dim Strsql As String
    Dim Primary_Code As Integer = -1
    Dim on_load As Boolean = True
#Region "Function"
    Sub Load_Category()
        Try
            strsql = "Select Cate_ID,Cate_Name from categories order by Cate_ID "
            Dim DT_Category As New DataTable
            DT_Category = _mysql.GetMYSQLDataTable(strsql, "categories")
            With CboCategories
                .DataSource = DT_Category
                .DisplayMember = "Cate_Name"
                .ValueMember = "Cate_ID"
                .SelectedIndex = 0
            End With
        Catch ex As Exception
            frmDebug.lblFormName.Text = Me.Name
            frmDebug.lblFunctionName.Text = "Load_Category"
            frmDebug.MemoErr_Description.Text = ex.Message
            frmDebug.MemoSQL.Text = strsql
            frmDebug.ShowDialog()
        End Try
    End Sub
    Sub Load_Data(Optional ByVal StrCiteria As String = "")
        Try
            Strsql = "Select Section_ID,Section_Name,Section_Code,Cate_Name from sections left join categories  on sections.Cate_ID = categories.Cate_ID "
            Strsql = Strsql & " where sections.cate_ID = '" & Me.CboCategories.SelectedValue & "'"
            If StrCiteria <> "" Then
                Select Case SearchOption.EditValue
                    Case 0
                        Strsql = Strsql & " and Section_ID = " & StrCiteria

                    Case 1
                        Strsql = Strsql & " and Section_Name like '" & StrCiteria & "%'"
                End Select

            End If
            Strsql = Strsql & " order by Section_ID"
            Dim DT_Section As New DataTable
            DT_Section = _mysql.GetMYSQLDataTable(Strsql, "section")

            With GridSection
                .DataSource = DT_Section
                Me.GridColumn1.FieldName = "Section_ID"
                Me.GridColumn2.FieldName = "Section_Name"
                Me.GridColumn3.FieldName = "Cate_Name"
                Me.GridColumn4.FieldName = "Section_Code"
            End With
        Catch ex As Exception
            'Select Case (_mysql.MySQLExecuteScalar(Strsql))
            '    Case -1
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Data"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
                Exit Sub
            End With

            '    Case Else
            'MsgBox("ลบข้อมูลเรียบร้อยแล้ว", MsgBoxStyle.Information, "ผลการลบข้อมูล")


            'End Select
        End Try

    End Sub
#End Region

    Private Sub frmSection_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_Category()
        Load_Data()
        on_load = False
    End Sub

    Private Sub GridSection_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub MNAddSection_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MNAddSection.Click
        If frmNewSection.ShowDialog = Windows.Forms.DialogResult.Cancel Then Exit Sub
        Load_Data()
    End Sub

    Private Sub MnEditSection_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnEditSection.Click
        Try
            If Primary_Code = -1 Then Exit Sub
            Dim DT_Categories As New DataTable
            Strsql = "select Section_ID,Section_name,Section_Code,Cate_ID from sections where Section_ID = " & Primary_Code
            DT_Categories = _mysql.GetMYSQLDataTable(Strsql)
            With frmEditSection
                .TxtSectionName.Text = DT_Categories.Rows(0).Item("Section_name").ToString
                .TxtSectionCode.Text = DT_Categories.Rows(0).Item("Section_Code").ToString
                .Primary_Code = Primary_Code
                If .ShowDialog = Windows.Forms.DialogResult.Cancel Then Exit Sub

            End With
            Load_Data()
        Catch ex As Exception

            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "MnEditCategories_Click"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()

            End With

        End Try

    End Sub

    Private Sub MnDeleteSection_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnDeleteSection.Click
        Try
            If Primary_Code = -1 Then Exit Sub
            If (MsgBox("กรุณายืนยันการลบข้อมูล " & Primary_Code, MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบข้อมูล") = MsgBoxResult.No) Then Exit Sub
            If (MsgBox("การลบข้อมูลเมื่อยืนยันแล้วจะไม่สามารถนำกลับมาใหม่ได้ คุณยังต้องการยืนยันใช่หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบข้อมูล") = MsgBoxResult.No) Then Exit Sub
            Strsql = "Select count(*) as cc from sections where Section_ID = " & Primary_Code
            If (_mysql.MySQLExecuteScalar(Strsql)) = 0 Then
                MsgBox("ไม่พบรหัสที่ต้องการลบ", MsgBoxStyle.Critical, "กรุณาตรวจสอบรายการลบ")
                Exit Sub
            End If


            Strsql = "Delete from sections where  section_ID = '" & Primary_Code & "'"

            Select Case (_mysql.MySQLExecuteScalar(Strsql))
                Case -1
                    With frmDebug
                        .lblFormName.Text = Me.Name
                        .lblFunctionName.Text = "MnDeleteSection_Click"
                        .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                        .MemoSQL.Text = Strsql
                        .ShowDialog()
                        Exit Sub
                    End With

                Case Else
                    MsgBox("ลบข้อมูลเรียบร้อยแล้ว", MsgBoxStyle.Information, "ผลการลบข้อมูล")


            End Select

            'Add Log
            Strsql = "Insert into transections_logs (trans_code,trans_datetime,trans_computer,trans_item) values (7,'" & Pn_Framework.MysqlDateTimeFormat(Now, True) & "','" & Pn_Framework.Get_Computername & "','" & Primary_Code & "') "
            Select Case (_mysql.MySQLExecute(Strsql))
                Case -1
                    With frmDebug
                        .lblFormName.Text = Me.Name
                        .lblFunctionName.Text = "MnDeleteCategories_Click"
                        .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                        .MemoSQL.Text = Strsql
                        .ShowDialog()

                    End With
            End Select

            Load_Data()
        Catch ex As Exception
            ' MsgBox(ex.Message)
        End Try
    End Sub

 
    Private Sub BtnSearch_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BtnSearch.ItemClick
        Load_Data(Me.TxtSearch.EditValue)

    End Sub

    Private Sub GridSection_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridSection.Click

    End Sub

    Private Sub GridView1_CustomRowCellEditForEditing(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs) Handles GridView1.CustomRowCellEditForEditing
        Try

            Dim Gv As GridView = sender
            Dim CodeName As String

            'Dim TypeCODE As String = Gv.GetRowCellValue(e.RowHandle, Gv.Columns("Item_Type")).ToString()
            If Me.SearchOption.EditValue = 0 Then
                CodeName = Gv.GetRowCellValue(e.RowHandle, Gv.Columns("Section_ID")).ToString()
            Else
                CodeName = Gv.GetRowCellValue(e.RowHandle, Gv.Columns("Section_Name")).ToString()
            End If
            Primary_Code = Gv.GetRowCellValue(e.RowHandle, Gv.Columns("Section_ID")).ToString()

            Me.TxtSearch.EditValue = CodeName
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub CboCategories_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboCategories.SelectedIndexChanged
        If on_load = True Then Exit Sub
        Load_Data()
    End Sub
End Class